<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Base Protoweb's controller, It has commonly used methods
 *
 * Public controller methods are friendly urls thanks to {@link http://codeigniter.com
 * Codeigniter}.
 *
 * @author Gonzalez Federico, Viera Leonel
 * @package Protoweb
 * @subpackage controller
 */
class PwController extends CI_Controller {

  public function __construct() {
    parent::__construct();
  } 

  /**
   * Returns Protoweb's application directory path
   *
   * @return string
   */
  protected function app_path() {
    return (APPPATH);
  }

  /**
   * Returns Protoweb's config directory path
   *
   * @return string
   */
  protected function config_path() {
    return ($this->app_path() . "config/");
  }

  /**
   * Returns Protoweb's controllers directory path
   *
   * @return string
   */
  protected function controllers_path() {
    return ($this->app_path() . "controllers/");
  }

  /**
   * Returns Protoweb's models directory path
   *
   * @return string
   */
  protected function models_path() {
    return ($this->app_path() . "models/");
  }

  /**
   * Returns Protoweb's views directory path
   *
   * @return string
   */
  protected function views_path() {
    return ($this->app_path() . "views/");
  }

  /**
   * Instance a Widget with all of its plugin functionality
   *
   * @param integer $id ID of the widget to instance
   * @return Protoweb_plugin
   */
  public function get_widget ($id) {
    $widget = new Widget($id);

    $widget_attrs = array();
    foreach ($widget->widget_attribute as $w_attr) {
      $widget_attrs[$w_attr->key] = $w_attr->value;
    }

    include_once $this->config->item("plugins_path") . $widget->plugin->key_name . "/" . $widget->plugin->key_name . ".php";

    $plg = new $widget->plugin->key_name($widget_attrs);

    return($plg);
  }

  /**
   * Generates action HTML file in generated project
   *
   * @param integer $action_id ID of the action that needs to generate its view
   * @return boolean
   */
  public function generate_view_files($action_id) {
    $data["action"]  = new Action($action_id);
    $data["entity"]  = new Entity($data["action"]->entity->id);
    $data["widgets"] = array();

    foreach ($data["action"]->widget->order_by("order")->get() as $widget) {
      array_push($data["widgets"], $this->get_widget($widget->id));
    }

    $this->smartyview->smarty->assign($data);

    if ($data["action"]->purpose == "list")
      $view_generated = $this->smartyview->smarty->fetch($this->config->item("generators_path") . "view_template_listing.html");
    else
      $view_generated = $this->smartyview->smarty->fetch($this->config->item("generators_path") . "view_template_presentation.html");

    $file_path = $data["entity"]->project->views_path() . $data["entity"]->model_name . "_" . $data["action"]->name . ".html";

    if ( ! write_file($file_path, $view_generated, "w")) {
      return(FALSE);
    }
    else {
      chmod($file_path,0777);
    }

    return(TRUE);
  } // end of method generate_view_files($action_id)

} // class PwController

/* End of file protoweb.php */
/* Location: ./controllers/pwcontroller.php */
